package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.Map;

/**
 * @author xiaycbstart
 * @create 2019 12 30 16:17
 */
public interface UserDao {
    @Select("select * from t_user where username = #{username}")
    User findByUsername(String username);
    @Select("<script>" +
            "select * from t_user\n" +
            "<if test=\"value != null and value.length > 0\">\n" +
            "where name like '%' #{value} '%'" +
            "</if>" +
            "</script>")
    Page<User> pageQuery(String value);

    @Delete("delete from t_user where id =#{id}")
    void deleteById(int id);
    @Delete("delete from t_user_role where user_id = #{id}")
    void deleteRelationMemberAndRole(int id);
    @Insert("insert into t_user_role values(#{user_id},#{role_id})")
    void addRelationUserAndRole(Map map);
    @Insert("insert into t_user values(null,#{birthday},#{gender},#{username},#{password},#{remark},null,#{telephone})")
    @Options(useGeneratedKeys = true ,keyColumn = "id",keyProperty = "id")
    void add(User user);
    @Select("select role_id from t_user_role where user_id = #{id}")
    Integer[] findRoleIdsByUserId(Integer id);
    @Update("<script>" +
            "update t_user\n" +
            "<set>\n" +
            "<if test=\"birthday != null\">\n" +
            "birthday = #{birthday},\n" +
            "</if>\n" +
            "<if test=\"gender != null\">\n" +
            "gender = #{gender},\n" +
            "</if>\n" +
            "<if test=\"username != null\">\n" +
            "username = #{username},\n" +
            "</if>\n" +
            "<if test=\"password != null\">\n" +
            "password = #{password},\n" +
            "</if>\n" +
            "<if test=\"remark != null\">\n" +
            "remark = #{remark},\n" +
            "</if>\n" +
            "<if test=\"telephone != null\">\n" +
            "telephone = #{telephone},\n" +
            "</if>\n" +
            "</set>\n" +
            "where id = #{id}" +
            "</script>")
    void update(User user);
}
